package com.spic.system.activiti.service.impl;

import com.spic.common.core.utils.UUIDUtil;
import com.spic.system.activiti.domain.BusinessStatus;
import com.spic.system.activiti.enums.BusinessStatusEnum;
import com.spic.system.activiti.mapper.BusinessStatusMapper;
import com.spic.system.activiti.service.IBusinessStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BusinessStatusServiceImpl  implements IBusinessStatusService
{
    @Autowired
    private BusinessStatusMapper businessStatusMapper;
    @Override
    public int add(String businessKey,String businessType,String instanceId) {
        BusinessStatus bs = new BusinessStatus();
        String uuid = UUIDUtil.createUUID();
        // 待提交
        bs.setId(uuid);
        // 待提交 设置状态为Integer.valueOf(BusinessStatusEnum.WAIT.getDesc()
        bs.setStatus(BusinessStatusEnum.WAIT.getCode());
        bs.setCreateDate(new Date());
        bs.setBusinessKey(businessKey);
        bs.setBusinessType(businessType);
        bs.setProcessInstanceId(instanceId);
        return businessStatusMapper.insertBusinessStatus(bs);
    }

    @Override
    public void updateStateByInstanceId(String processInstanceId,BusinessStatusEnum statusEnum)
    {
        BusinessStatus bs = new BusinessStatus();
        bs.setProcessInstanceId(processInstanceId);
        bs.setStatus(statusEnum.getCode());
        businessStatusMapper.updateStateByInstanceId(bs);

    }

    /**
     * 根据businessType 查询状态
     * @param businessType
     * @return
     */
    @Override
    public List<BusinessStatus> selectStatusByType(String businessType)
    {
        BusinessStatus businessStatus = new BusinessStatus();
        businessStatus.setBusinessType(businessType);
        List<BusinessStatus>  liststatus = businessStatusMapper.selectStatus(businessStatus);
        return liststatus;
    }

    @Override
    public List<BusinessStatus> selectStatus(String businessType, List<String> businessKeys) {
        return businessStatusMapper.selectStatusByIds(businessType, businessKeys);
    }

    @Override
    public List<BusinessStatus> selectByInstanceId(List<String> processInstanceId) {
        return businessStatusMapper.selectByInstanceId(processInstanceId);
    }

    @Override
    public List<BusinessStatus> selectBusinessStatusList(BusinessStatus businessStatus)
    {
        return businessStatusMapper.selectBusinessStatusList(businessStatus);
    }


}
